<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="14">
                    <a-form-item label="已结算金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['settlementAmount']" placeholder="请输入已结算金额"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="10">
                    <a-form-item label="总金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['totalAmount']" placeholder="请输入总金额"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="14">
                    <a-form-item label="未结算金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['amountOutstanding']" placeholder="请输入未结算金额"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="10">
                    <a-form-item label="引用单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['referenceNumber']" placeholder="请输入引用单号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="14">
                    <a-form-item label="本次结算金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['amountThisSettlement']" placeholder="请输入本次结算金额"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="10">
                    <a-form-item label="选择" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['choose']" placeholder="请输入选择"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="14">
                    <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['note']" placeholder="请输入备注"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="10">
                    <a-form-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['documentDate']" placeholder="请输入单据日期"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction
} from '@/api/manage'
import pick from 'lodash.pick'

export default {
    name: "PurchasingPaymentSubModal",
    components: {},
    props: {
        mainId: {
            type: String,
            required: false,
            default: ''
        }
    },
    data() {
        return {
            form: this.$form.createForm(this),
            title: "操作",
            width: 800,
            visible: false,
            model: {},
            labelCol: {
                sm: {
                    span: 6
                },
            },
            wrapperCol: {
                sm: {
                    span: 16
                },
            },

            confirmLoading: false,
            validatorRules: {},
            url: {
                add: "/procurement/purchasingPayment/addPurchasingPaymentSub",
                edit: "/procurement/purchasingPayment/editPurchasingPaymentSub",
            }

        }
    },
    created() {},
    methods: {
        add() {
            this.edit({});
        },
        edit(record) {
            this.form.resetFields();
            this.model = Object.assign({}, record);
            this.visible = true;
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'referenceNumber', 'documentDate', 'totalAmount', 'settlementAmount', 'amountOutstanding', 'amountThisSettlement', 'choose', 'note'))
            })
        },
        close() {
            this.$emit('close');
            this.visible = false;
        },
        handleOk() {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }

                    let formData = Object.assign(this.model, values);
                    formData['purchasingPaymentId'] = this.mainId
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        that.close();
                    })
                }

            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'referenceNumber', 'documentDate', 'totalAmount', 'settlementAmount', 'amountOutstanding', 'amountThisSettlement', 'choose', 'note'))
        },

    }
}
</script>
